﻿using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Text;


[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000)]
public struct CSV : IBinarySerialize
{
    private StringBuilder values;

    public void Init()
    {
        this.values = new StringBuilder();
    }

    public void Accumulate(SqlString Value, SqlString Seperator)
    {
        this.values.Append(Value.Value);
        this.values.Append(Seperator.Value);
    }

    public void Merge(CSV Group)
    {
        this.values.Append(Group.values.ToString());
    }

    public SqlString Terminate()
    {
        return new SqlString(values.ToString());
    }

    public void Read(BinaryReader r)
    {
        this.values = new StringBuilder(r.ReadString());
    }

    public void Write(BinaryWriter w)
    {
        w.Write(values.ToString());
    }
}
